package com.mmlc.meimei.lichengkongjian.been;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mmlc.meimei.lichengkongjian.mine.LoginActivity;
import com.mmlc.network.filter.LoginRequestFilter;
import com.mmlc.network.filter.UserDetailFilter;
import com.mmlc.network.responsebean.LoginResponseBean;
import com.mmlc.utils.EditInputUtils;
import com.mmlc.utils.SPUtils;
import com.mmlc.utils.ToastUtils;

/**
 * Created by Administrator on 2016/7/20 0020.
 */
public class User {
    private static final String USER_PASSWORD = "USER_PASSWORD";
    private static final String USER_NICKNAME = "USER_NICKNAME";
    private static final String USER_SEX = "USER_SEX";
    private static final String USER_LOGIN_STATE = "USER_LOGIN_STATE";
    private static final String USER_PHONE = "USER_PHONE";
    private boolean loginState = false;//登陆状态
    private static final String USER_PHOTOPATH = "USER_PHOTOPATH";
    private String password;//登陆密码
    private String phone;//手机号
    private Context context;



    public String getPhone() {
        String phone = (String) SPUtils.get(context, USER_PHONE, "");
        return phone;
    }

    public void setPhone(String phone) {
        SPUtils.put(context, USER_PHONE, phone);
    }

    public User(Context context) {

        this.context = context;
    }

    /**
     * 获取用户的登陆状态
     */
    public boolean getLoginState() {
        boolean state = (Boolean) SPUtils.get(context, USER_LOGIN_STATE, false);
        return state;
    }

    public void setLoginState(boolean loginState) {
        SPUtils.put(context, USER_LOGIN_STATE, loginState);
    }

    /**
     * 获取用户的密码
     */
    public String getPassword() {
        String password = (String) SPUtils.get(context, USER_PASSWORD, "");
        return password;
    }

    /**
     * 保存用户的密码
     */
    public void setPassword(String password) {
        SPUtils.put(context, USER_PASSWORD, password);
    }
    public void removePassword() {
        SPUtils.remove(context, USER_PASSWORD);
    }

    /**
     * 保存用户的昵称
     */
    public void saveNickName(String nickName) {
        SPUtils.put(context, USER_NICKNAME, nickName);
    }

    /**
     * 获取用户的昵称
     */
    public String getNickName() {
        String nickName = (String) SPUtils.get(context, USER_NICKNAME, "");
        return nickName;
    }

    /**
     * 保存用户的性别
     * 0:保密
     * 1：男
     * 2：女
     */
    public void saveSex(int sex) {
        SPUtils.put(context, USER_SEX, sex);
    }

    /**
     * 获取用户的性别
     * 0:保密
     * 1：男
     * 2：女
     */
    public String getSex() {
        int sex = (Integer) SPUtils.get(context, USER_SEX, 0);
        String tempSex = "";
        if (sex == 0) {
            tempSex = "保密";
        } else if (sex == 1) {
            tempSex = "男";
        } else if (sex == 2) {
            tempSex = "女";
        }else{
            tempSex = "保密";
        }
        return tempSex;
    }

    /**
     * 获取用户头像
     */
    public String getPhoto( ) {
        return (String) SPUtils.get(context, USER_PHOTOPATH, "");
       /* String tmpPhotoPath = MD5Utils.keyToMD5(photoPath);
        Bitmap cacheBitmap = ConfigUtil.fileCacheFactory.sdcardCache.getCacheBitmap(tmpPhotoPath);
        if (cacheBitmap != null) {
            imageView.setImageBitmap(cacheBitmap);
        } else {
            loadHeaderViewFromNet(imageView, photoPath);
        }*/
    }

    /**
     * 保存用户头像
     */
    public void savePhoto(String photoPath) {
        //保存头像的http地址，方便之后在缓存找不到的请求下去获取
        SPUtils.put(context, USER_PHOTOPATH, photoPath);

       /* photoPath = MD5Utils.keyToMD5(photoPath);
        ConfigUtil.fileCacheFactory.sdcardCache.cacheBitmap(photoPath, ImageTools.bitmapToBytes(bitmap));*/
    }

    /**
     * 请求登陆
     */
    public void requestLogin(String phoneNumber, String passWord, BaseParentActivity activity, BaseRequestFilterLayer.AccessResultFromFilter<LoginResponseBean> callBack) {
        //检查手机号的合法性
        boolean isPhone = EditInputUtils.checkedPhoneNumber(phoneNumber);
        if (!isPhone) {
            ToastUtils.showToast(activity, "请输入正确的手机号");
            return;
        }
        //检查密码是否符合要求
//        boolean isPassword = EditInputUtils.checkPassword(activity, passWord);
//        if (!isPassword) {
//            ToastUtils.showToast(activity, "密码只能以字符开头，其后跟字母，数字，下划线");
//            return;
//        }
        //发送登陆的网络请求
        LoginRequestFilter filter = new LoginRequestFilter(activity);
        filter.requestBean.method = "login";
        filter.requestBean.phone = phoneNumber;
        filter.requestBean.password = passWord;
        filter.setDebug(false);//表示请求真正的服务端接口
        filter.sendRequest(callBack);
    }

    /**
     * 从网络加载头像
     */
    public void loadHeaderViewFromNet(final ImageView headView, final String headPath) {
        if (TextUtils.isEmpty(headPath)) {
            return;
        }
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, headPath, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                byte[] bytes = responseInfo.result.getBytes();
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                if (bitmap == null) {
                    return;
                }
                if (headView != null) {
                    headView.setImageBitmap(bitmap);
                }
                //保存头像
              /*  savePhoto(bitmap, headPath);*/

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    public void getUserDetail(BaseParentActivity activity) {
        UserDetailFilter filter = new UserDetailFilter(activity);
        filter.requestBean.method = "getUserInfo";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<Object>() {

            @Override
            public void onSuccess(Object result) {

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    public void jmpToLogin(Activity activity) {
        Intent login = new Intent(context, LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(login);
    }

    public void saveUserInfo(String phone,String password,String photoPath){
        setPhone(phone);
        setPassword(password);
        if(TextUtils.isEmpty(photoPath)){
            photoPath ="";
        }
        savePhoto(photoPath);
    }


    public void setShopName(String name){
        if(name == null){
            name = "";
        }
        SPUtils.put(context, "user_shopName", name);
    }
    public String getShopName( ){
        return (String)SPUtils.get(context, "user_shopName", "");
    }

    public void setUserType(int type){
        SPUtils.put(context, "user_type", type);
    }
    public Integer getUserType(){
        return (Integer)SPUtils.get(context, "user_type",0);
    }

    public void setUserId(String id){
        SPUtils.put(context, "user_id", id);
    }
    public String getUserId(){
        return (String)SPUtils.get(context, "user_id","");
    }


    public void setIsTuiDan(int type){
        SPUtils.put(context, "is_tuidan", type);
    }
    public Integer getIsTuiDan(){
        return (Integer)SPUtils.get(context, "is_tuidan",0);
    }

    public void setIsStore(int type){
        SPUtils.put(context, "is_store", type);
    }
    public Integer getIsStore(){
        return (Integer)SPUtils.get(context, "is_store",0);
    }

}
